<template>
  <el-card :body-style="{ padding: '0px' }" class="knowledge-card" shadow="hover">
    <!-- 背景图和图标 -->
    <div class="card-header" :style="{ background: card.bgColor }">
      <i :class="card.icon" class="card-icon"></i>
    </div>

    <!-- 卡片内容 -->
    <div class="card-content">
      <div class="card-title">
        <span>{{ card.name }}</span>
        <el-dropdown trigger="click" class="dropdown">
          <i class="el-icon-more"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleEdit">编辑</el-dropdown-item>
            <el-dropdown-item @click.native="handleDelete">删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <p class="card-desc">{{ card.description }}</p>
      <div class="card-meta">

        <span><i style="margin-right: 5px;" class="el-icon-document" />{{ card.fileCount }} 个文件</span>
        <span>更新于 {{ card.updateTimeStr }}</span>
      </div>
    </div>

    <!-- 编辑知识库弹窗 -->
    <KnowledgeEditModal :visible.sync="showEdit" @submit="handleEditKnowledge" :dataInfo="dataInfo"/>
  </el-card>
</template>

<script>
import { deleteKnowledge,handleEditKnowledge } from '@/api/knowledge'
import KnowledgeEditModal from './KnowledgeEditModal.vue'
export default {
  name: 'KnowledgeCard',
  components: { KnowledgeEditModal },
  props: {
    card: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      showEdit: false, // 控制编辑弹窗显示
      dataInfo: {}
    }
  },
  methods: {
    /**
     * 打开编辑弹窗
     */
    handleEdit() {
      this.dataInfo = JSON.parse(JSON.stringify(this.card))
      this.showEdit = true
    },
    /**
     * 知识库重命名
     */
    async handleEditKnowledge(data) {
      console.log('编辑知识库:')
      console.log(data)
      const param = {
        ...data
      }
      const  res = await handleEditKnowledge(param);
      if(res && res.data.code === 200) {
        this.$message.success('知识库编辑成功')
        this.showEdit = false // 关闭编辑弹窗
        this.$emit('loadding') // 通知父组件刷新列表
      } else {
        this.$message.error(res.data.message || '知识库编辑失败，请稍后重试')
      }
    },
    /**
     * 删除知识库
     */
    handleDelete() {
      const that = this
      this.$confirm('此操作将永久删除该知识库, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          knowledgeCode: that.card.knowledgeCode
        }
        deleteKnowledge(param).then(res => {
          if (res.data.code === 200) {
            that.$message.success('知识库删除成功')
            that.$emit('loadding') // 通知父组件刷新列表
          } else {
            that.$message.error('知识库删除失败，请稍后重试')
          }
        }).catch(err => {
          console.error('删除知识库失败:', err)
          that.$message.error('网络错误，请稍后重试')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    }
  }
}
</script>

<style scoped>
.knowledge-card {
  width: 100%;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s;
}

.card-header {
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.card-icon {
  font-size: 32px;
  color: white;
}

.card-content {
  padding: 16px;
}

.card-title {
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.card-desc {
  color: #666;
  font-size: 14px;
  margin-bottom: 12px;
}

.card-meta {
  font-size: 12px;
  color: #999;
  display: flex;
  justify-content: space-between;
}

.dropdown {
  cursor: pointer;
}
</style>
